#include <stdio.h>
#include <threads.h>

int data1 = -1, data2 = -1;
mtx_t mtx;
cnd_t cnd;

static int thrd_task1(void *arg)
{
    thrd_sleep(&(struct timespec){3, 0}, NULL);
    data1 = 12000;

    if (data2 != -1)
        cnd_signal(&cnd);
    else
    {
        mtx_lock(&mtx);
        cnd_wait(&cnd, &mtx);
        mtx_unlock(&mtx);
    }
    printf("%d\n", data1 + data2);
    return 0;
}

static int thrd_task2(void *arg)
{
    thrd_sleep(&(struct timespec){2, 0}, NULL);
    data2 = 306;
    if (data1 != -1)
        cnd_signal(&cnd);
    else
    {
        mtx_lock(&mtx);
        cnd_wait(&cnd, &mtx);
        mtx_unlock(&mtx);
    }
    printf("%d\n", data1 + data2);
    return 0;
}

int main()
{
    thrd_t t0, t1;
    mtx_init(&mtx, mtx_plain);
    cnd_init(&cnd);
    thrd_create(&t0, thrd_task1, NULL);
    thrd_create(&t1, thrd_task2, NULL);
    thrd_join(t0, NULL);
    thrd_join(t1, NULL);
    mtx_destroy(&mtx);
    cnd_destroy(&cnd);
}